package homework;
//作业标题(2307)
//判定链表是否是回文
//
//作业内容
//判定链表是否是回文
//https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa?tpId=49&&tqId=29370&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking
public class h2307 {

public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
    public boolean chkPalindrome(ListNode A) {
        // write code here
        if (A == null || A.next == null) return true;

        ListNode fast = A;
        ListNode last = A;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            last = last.next;
        }

        ListNode head = last;

        ListNode cur = last.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }

        while (A != null && head != null) {
            if (A.val != head.val) {
                return false;
            }
            A = A.next;
            head = head.next;
        }
        return true;
    }
}
